const app = getApp();
import * as API_Members from '../../api/members'
import * as API_Distribution from '../../api/distribution'
import { Foundation } from '../../ui-utils/index.js'

Page({
  data: {
    showType: 0,
    points: {
      consum_point:'获取中...',
      grade_point:'获取中...'
    },
    params: {
      page_no: 1,
      page_size: 20
    },
    pointsList:[],
    finished: false,
    showGoTop: false,
    scrollTop: 0,//滚动高度
    scrollHeight: '',
    pageCount: 0,
    msg: ''
  },
  switchTab(event) {
    let index = parseInt(event.currentTarget.dataset.index)
    this.setData({
      'params.page_no': 1,
      pointsList: [],
      finished: false
    })
    this.GET_PromoteDetails(index==0?1:0)
    this.setData({showType: index})
  },

  onLoad: function (options) {
   this.GET_PromoteDetails(1)
    this.setData({
      scrollHeight: wx.getSystemInfoSync().windowHeight - 84
    })
  },
  GET_PromoteDetails(is_withdraw) {
    API_Distribution.getPromoteDetails({
      is_withdraw:is_withdraw,
      page_no: this.data.params.page_no,
      page_size: this.data.params.page_size
    }).then(response => {
      console.log(response)
      const { data } = response;
      if (!data || !data.length) {
        this.setData({finished: true,msg : '没有更多内容了' })
      } else {
        data.forEach(key=>{
          key.create_time = Foundation.unixToDate(key.create_time)
        })
        this.setData({ pointsList: this.data.pointsList.concat(data)})
        if(response.data_total<response.page_size){
          this.setData({ finished : true , msg : '没有更多内容了' })
        }
      }
    });
  },

  loadMore: function () {
    if (!this.data.finshed) {
      this.setData({ "params.page_no": this.data.params.page_no + 1 })
      this.GET_PromoteDetails(this.data.showType==1?0:1)   
    }
    else{
      return
    }
  },
  scroll: function (e) {
    let that = this
    if (e.detail.scrollTop > 200) {
      that.setData({showGoTop: true})
    } else {
      that.setData({showGoTop: false})
    }
  },
  //返回顶部
  goTop: function () { this.setData({ scrollTop: 0 })}
})